using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace BTSDecompress
{
    public partial class fdlgParseXML : Form
    {
        public fdlgParseXML()
        {
            InitializeComponent();
        }

        private void btnParse_Click(object sender, EventArgs e)
        {
            txtOutput.Text = parseXML(txtXML.Text, chkInnerOuterXML.Checked);
        }

        private string parseXML(string XML, bool bHideOuterXMLTag)
        {
            string res = null;

            try
            {
                XmlDocument o = new XmlDocument();
                o.LoadXml(XML);
                XmlNamespaceManager manager = new XmlNamespaceManager(o.NameTable);

                if (nameSpaceTextBox.Text.Length > 0 && preFixTextBox.Text.Length > 0)
                {
                    manager.AddNamespace(preFixTextBox.Text, nameSpaceTextBox.Text);
                }

                if (bHideOuterXMLTag)
                {
                    if (nameSpaceTextBox.Text.Length > 0 && preFixTextBox.Text.Length > 0)
                    {
                        res = o.SelectSingleNode(txtQuery.Text, manager).InnerXml;
                    }
                    else
                    {
                        res = o.SelectSingleNode(txtQuery.Text).InnerXml;
                    }
                }
                else
                {
                    if (nameSpaceTextBox.Text.Length > 0 && preFixTextBox.Text.Length > 0)
                    {
                        res = o.SelectSingleNode(txtQuery.Text, manager).OuterXml;
                    }
                    else
                    {
                        res = o.SelectSingleNode(txtQuery.Text).OuterXml;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to parse: " + ex.Message);
            }
            return res;




        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}